package com.visikard.ui.activity;

import android.content.ActivityNotFoundException;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.visikard.ui.R;
import com.visikard.utility.Utils;

public class CouponBuilderActivityV2 extends BaseFragmentActivity {

	// keep track of camera capture intent
	final int CAMERA_CAPTURE = 1;
	final int PICK_GALLERY = 2;
	
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.coupon_builder_24v2);
		((Button) findViewById(R.id.button_camera))
				.setOnClickListener(new MyOnClickListener());
		((Button) findViewById(R.id.button_gallery))
				.setOnClickListener(new MyOnClickListener());
		((Button) findViewById(R.id.button_back))
				.setOnClickListener(new MyOnClickListener());

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			if (requestCode == CAMERA_CAPTURE) {
				
				Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, Media.DATE_ADDED, null, "date_added DESC");
		        if(cursor != null && cursor.moveToFirst())
		        {
		        	String picturePath="";
		        	
		            do 
		            {
		                picturePath =cursor.getString(cursor.getColumnIndex(Media.DATA));		               
						break;
		            }
		            while(cursor.moveToNext());
		            cursor.close();		      
					Bundle bundle = new Bundle();
					bundle.putString("path",picturePath);
					Intent intent = new Intent(CouponBuilderActivityV2.this,
							CouponCameraPreviewActivity.class);
					intent.putExtras(bundle);
					startActivity(intent);        
		        }
		        
				

			} else if (requestCode == PICK_GALLERY) {
				Uri selectedImage = data.getData();
				String[] filePathColumn = { MediaStore.Images.Media.DATA };

				Cursor cursor = getContentResolver().query(selectedImage,
						filePathColumn, null, null, null);
				cursor.moveToFirst();

				int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
				String picturePath = cursor.getString(columnIndex);
				
				cursor.close();
			
				Bundle bundle = new Bundle();
				bundle.putString("path",picturePath);
				
				Intent intent = new Intent(CouponBuilderActivityV2.this,
						CouponCameraPreviewActivity.class);
				intent.putExtras(bundle);
				startActivity(intent);
				
			}
		} else if (resultCode == RESULT_CANCELED) {
			
		}

	}

	private class MyOnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.button_back:
				finish();
				break;
			case R.id.button_camera:
				try {
					ContentValues values = new ContentValues();
					values.put(MediaStore.Images.Media.TITLE, "Image File name");
					Uri mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
					Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
					intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
					startActivityForResult(intentPicture,CAMERA_CAPTURE);
				} catch (ActivityNotFoundException anfe) {
					// display an error message
					String errorMessage = "Whoops - your device doesn't support capturing images!";
					Utils.showToast(CouponBuilderActivityV2.this, errorMessage);

				}
				break;
			case R.id.button_gallery:
				try {
					Intent i = new Intent(
							Intent.ACTION_PICK,
							android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
					startActivityForResult(i, PICK_GALLERY);
				} catch (ActivityNotFoundException anfe) {
					// display an error message
					String errorMessage = "Whoops - your device doesn't support capturing images!";
					Utils.showToast(CouponBuilderActivityV2.this, errorMessage);

				}
				break;
			default:
				break;
			}
		}

	}
	
	
	public String getRealPathFromURI(Uri contentUri)
    {
        try
        {
            String[] proj = {MediaStore.Images.Media.DATA};
            Cursor cursor = managedQuery(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        catch (Exception e)
        {
            return contentUri.getPath();
        }
    }
}
